<?php
require_once 'vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;
$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => '129.226.194.102',
    'database'  => 'aimeos_lvgang_to',
    'username'  => 'aimeos_lvgang_to',
    'password'  => 'ZQaJw6X9nksn8kt7',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

try {
    $pdo = $capsule->getConnection()->getPdo();
    
    // Create mshop_attribute_type table
    $sql = "CREATE TABLE IF NOT EXISTS mshop_attribute_type (
        id INT AUTO_INCREMENT PRIMARY KEY,
        siteid VARCHAR(255) NOT NULL DEFAULT '',
        domain VARCHAR(32) NOT NULL,
        code VARCHAR(64) NOT NULL,
        label VARCHAR(255) NOT NULL,
        pos INT NOT NULL DEFAULT 0,
        status SMALLINT NOT NULL DEFAULT 1,
        i18n TEXT,
        mtime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        ctime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        editor VARCHAR(255) NOT NULL DEFAULT '',
        UNIQUE KEY unq_msattty_sid_dom_code (siteid, domain, code),
        KEY idx_msattty_status_pos (status, pos)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
    
    $pdo->exec($sql);
    echo "mshop_attribute_type table created successfully\n";
    
    // Create mshop_attribute table
    $sql = "CREATE TABLE IF NOT EXISTS mshop_attribute (
        id INT AUTO_INCREMENT PRIMARY KEY,
        siteid VARCHAR(255) NOT NULL DEFAULT '',
        type VARCHAR(64) NOT NULL,
        domain VARCHAR(32) NOT NULL,
        code VARCHAR(255) NOT NULL,
        label VARCHAR(255) NOT NULL,
        pos INT NOT NULL DEFAULT 0,
        status SMALLINT NOT NULL DEFAULT 1,
        mtime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        ctime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        editor VARCHAR(255) NOT NULL DEFAULT '',
        UNIQUE KEY unq_msatt_sid_dom_code (siteid, domain, code),
        KEY idx_msatt_sid_status (siteid, status),
        KEY idx_msatt_sid_label (siteid, label),
        KEY idx_msatt_sid_pos (siteid, pos)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
    
    $pdo->exec($sql);
    echo "mshop_attribute table created successfully\n";
    
    // Insert default attribute types
    $insertSql = "INSERT IGNORE INTO mshop_attribute_type (siteid, domain, code, label, pos, status, editor) VALUES 
        ('', 'product', 'color', 'Color', 0, 1, 'setup'),
        ('1.', 'product', 'color', 'Color', 0, 1, 'setup'),
        ('', 'product', 'size', 'Size', 1, 1, 'setup'),
        ('1.', 'product', 'size', 'Size', 1, 1, 'setup'),
        ('', 'product', 'width', 'Width', 2, 1, 'setup'),
        ('1.', 'product', 'width', 'Width', 2, 1, 'setup'),
        ('', 'product', 'length', 'Length', 3, 1, 'setup'),
        ('1.', 'product', 'length', 'Length', 3, 1, 'setup'),
        ('', 'product', 'print', 'Print', 4, 1, 'setup'),
        ('1.', 'product', 'print', 'Print', 4, 1, 'setup'),
        ('', 'product', 'sticker', 'Sticker', 5, 1, 'setup'),
        ('1.', 'product', 'sticker', 'Sticker', 5, 1, 'setup');";
    
    $pdo->exec($insertSql);
    echo "Default attribute type data inserted successfully\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}